package com.bestcem.xm.ticket.dao.impl.mysql;

import cn.hutool.core.lang.ObjectId;
import com.bestcem.xm.common.core.uitls.DateUtil;
import com.bestcem.xm.ticket.dao.TicketTemplateDao;
import com.bestcem.xm.ticket.dao.converter.TicketTemplateConverter;
import com.bestcem.xm.ticket.dao.mapper.TicketTemplateDaoMapper;
import com.bestcem.xm.ticket.entity.mongo.TicketTemplate;
import com.bestcem.xm.ticket.entity.mysql.TicketTemplateDO;
import org.springframework.stereotype.Repository;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author zhangfeng <feng.zhang@idiaoyan.com>
 * @version v1.0
 * @date 2021/9/23 13:45
 * 2021年11月23日10:47:38： 原有ds_ticket_tickettemplate表已弃用，请使用ds_ticket_template_v2表
 * @see com.bestcem.xm.ticket.dao.TemplateDao
 */
@Repository
public class TicketTemplateDaoImpl implements TicketTemplateDao {
    @Resource
    private TicketTemplateDaoMapper ticketTemplateDaoMapper;

    @Override
    @Deprecated
    public TicketTemplate findById(String id) {
        TicketTemplateDO ticketTemplateDO = ticketTemplateDaoMapper.findByTicketTemplateId(id);

        return TicketTemplateConverter.doToDto(ticketTemplateDO);
    }

    @Override
    @Deprecated
    public TicketTemplate saveTicketTemplate(TicketTemplate ticketTemplate) {
        TicketTemplateDO entityDo = TicketTemplateConverter.dtoToDo(ticketTemplate);
        if (Objects.isNull(entityDo)) {
            return null;
        }

        if (ObjectUtils.isEmpty(entityDo.getTicketTemplateId())) {
            entityDo.setTicketTemplateId(ObjectId.next());
        }
        if (Objects.isNull(entityDo.getCreateTime())) {
            entityDo.setCreateTime(DateUtil.getCommonDate());
        }
        if (Objects.isNull(entityDo.getUpdateTime())) {
            entityDo.setUpdateTime(DateUtil.getCommonDate());
        }
        ticketTemplateDaoMapper.insert(entityDo);

        return TicketTemplateConverter.doToDto(entityDo);
    }

    @Override
    @Deprecated
    public TicketTemplate findByConfigId(String configId) {
        TicketTemplateDO ticketTemplateDO = ticketTemplateDaoMapper.findByTicketConfigId(configId);

        return TicketTemplateConverter.doToDto(ticketTemplateDO);
    }

    @Override
    @Deprecated
    public TicketTemplate updateTemplate(String id, TicketTemplate ticketTemplate) {
        if (Objects.isNull(ticketTemplate)) {
            return null;
        }
        TicketTemplateDO entityDo = TicketTemplateConverter.dtoToDo(ticketTemplate);
        if (Objects.isNull(entityDo)) {
            return null;
        }
        entityDo.setUpdateTime(DateUtil.getCommonDate());

        ticketTemplateDaoMapper.updateByTicketTemplateId(entityDo);

        return TicketTemplateConverter.doToDto(entityDo);
    }
}
